package com.hopu.oa.controller.indexController;

import com.hopu.oa.dao.address.AddressDao;
import com.hopu.oa.dao.daymanage.DayManageDao;
import com.hopu.oa.dao.daymanage.MyCalendarDao;
import com.hopu.oa.dao.file.FileListDao;
import com.hopu.oa.dao.notice.AoaNoticeListDao;
import com.hopu.oa.dao.notice.NoticeManageDao;
import com.hopu.oa.entity.address.Director;
import com.hopu.oa.entity.daymanage.AoaScheduleList;
import com.hopu.oa.entity.file.FileList;
import com.hopu.oa.entity.mail.Mailreciver;
import com.hopu.oa.entity.management.AoaAttendsList;
import com.hopu.oa.entity.notice.AoaNoticeList;
import com.hopu.oa.entity.plan.AoaPlanList;
import com.hopu.oa.entity.process.ProcessAll;
import com.hopu.oa.entity.task.TongJiWangCeng;
import com.hopu.oa.entity.user.AoaNotepaper;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.daymanage.DayManageService;
import com.hopu.oa.service.impl.mail.mailServiceImpl;
import com.hopu.oa.service.impl.notice.AoaNoticeListServiceImpl;
import com.hopu.oa.service.impl.plan.AoaPlanListImpl;
import com.hopu.oa.service.impl.process.auditServiceImpl;
import com.hopu.oa.service.impl.task.TaskServiceImpl;
import com.hopu.oa.service.impl.user.UserPlaneServiceImp;
import com.hopu.oa.service.impl.user.UserServiceImpl;
import com.hopu.oa.service.management.AoaAttendsListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class index {
    @Autowired
    private AoaAttendsListService aoaAttendsListService;
    @Autowired
    private FileListDao fileListDao;
    @Autowired
    AddressDao addressDao;
    @Autowired
    private mailServiceImpl mailService;
    @Autowired
    private AoaNoticeListServiceImpl aoaNoticeListService;
    @Autowired
    private AoaPlanListImpl aoaPlanList;
    //用户标签
    @Autowired
    private UserPlaneServiceImp userPlaneServiceImp;
    @Autowired
    private auditServiceImpl auditService;
    @Autowired
    private MyCalendarDao myCalendarDao;
    @Autowired
    private AoaNoticeListDao aoaNoticeListDao;
    @Autowired
    private NoticeManageDao noticeManageDao;
    @Autowired
    private DayManageDao dayManageDao;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private TaskServiceImpl taskService;
    @Autowired
    private DayManageService dayManageService;

    /**
     * 首页统计图 ajax取值
     *
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/countweeklogin")
    public List<Integer> countweeklogin(HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        //如下取本周周一的时间点
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        //周一
        Date time1 = cal.getTime();
        //下面的+1，是取周2到周日
        cal.add(Calendar.DAY_OF_MONTH, +1);
        Date time2 = cal.getTime();
        cal.add(Calendar.DAY_OF_MONTH, +1);
        Date time3 = cal.getTime();
        cal.add(Calendar.DAY_OF_MONTH, +1);
        Date time4 = cal.getTime();
        cal.add(Calendar.DAY_OF_MONTH, +1);
        Date time5 = cal.getTime();
        cal.add(Calendar.DAY_OF_MONTH, +1);
        Date time6 = cal.getTime();
        cal.add(Calendar.DAY_OF_MONTH, +1);
        Date time7 = cal.getTime();
        //转格式
        String format1 = new SimpleDateFormat("yyyy-MM-dd").format(time1);
        String format2 = new SimpleDateFormat("yyyy-MM-dd").format(time2);
        String format3 = new SimpleDateFormat("yyyy-MM-dd").format(time3);
        String format4 = new SimpleDateFormat("yyyy-MM-dd").format(time4);
        String format5 = new SimpleDateFormat("yyyy-MM-dd").format(time5);
        String format6 = new SimpleDateFormat("yyyy-MM-dd").format(time6);
        String format7 = new SimpleDateFormat("yyyy-MM-dd").format(time7);
        //取改时间点的该用户的进入次数
        int size1 = userService.selectAllUser(format1, user.getUserId()).size();
        int size2 = userService.selectAllUser(format2, user.getUserId()).size();
        int size3 = userService.selectAllUser(format3, user.getUserId()).size();
        int size4 = userService.selectAllUser(format4, user.getUserId()).size();
        int size5 = userService.selectAllUser(format5, user.getUserId()).size();
        int size6 = userService.selectAllUser(format6, user.getUserId()).size();
        int size7 = userService.selectAllUser(format7, user.getUserId()).size();
        ArrayList<Integer> integers = new ArrayList<>();
        //将7天的次数，装进集合（前端js需要的格式）
        Collections.addAll(integers, size1, size2, size3, size4, size5, size6, size7);
        return integers;
    }

    @ResponseBody
    @RequestMapping("/counttasknum")
    public List<String> counttasknum() {
        ArrayList<String> strings = new ArrayList<>();
        //取每个用户的任务完成的数（type=7的），倒序取最多的5个用户，姓名id和完成数放入集合
        List<TongJiWangCeng> tongJiWangCengs = taskService.selectTongJi();
        for (TongJiWangCeng tongJiWangCeng : tongJiWangCengs) {
            //存入集合格式为（“ ”+姓名+“ :”+完成数），前端js需要的格式
            AoaUser aoaUser = userService.queryUserById(tongJiWangCeng.getUId());
            strings.add(" " + aoaUser.getUserName() + " :" + tongJiWangCeng.getSize());
        }
        return strings;
    }

    @ResponseBody
    @RequestMapping("/littlecalendar")
    public List<String> littlecalendar(HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        ArrayList<String> strings = new ArrayList<>();
        //取所有的该用户的日历集合
        List<AoaScheduleList> aoaScheduleLists = dayManageService.selectAll(user.getUserId(), null, 0, 0);

        for (AoaScheduleList aoaScheduleList : aoaScheduleLists) {
            //取开始时间StartTime，分割只要格式yyyy-MM-dd（前端需要的格式）
            String startTime = aoaScheduleList.getStartTime();
            String[] s = startTime.split(" ");
            strings.add(s[0]);
        }
        return strings;
    }

    /**
     * 控制面板主页
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/test2")
    public String tiao(Model model, HttpSession session) {
        //签到
        AoaUser aoaUser = ((AoaUser) session.getAttribute("user"));
        Integer userId = aoaUser.getUserId();
        Date date = new Date();
        String format = new SimpleDateFormat("yyyy-MM-dd").format(date);
        List<AoaAttendsList> aoaAttendsLists = aoaAttendsListService.selectByUserIDAndTime(format, userId);
        if (aoaAttendsLists.size() == 1) {
            AoaAttendsList aoaAttendsList1 = aoaAttendsLists.get(0);
            model.addAttribute("type", "上班");
            model.addAttribute("aalist", aoaAttendsList1);
        } else if (aoaAttendsLists.size() == 2) {
            AoaAttendsList aoaAttendsList2 = aoaAttendsLists.get(1);
            model.addAttribute("type", "下班");
            model.addAttribute("aalist", aoaAttendsList2);
        }
        //文件
        List<FileList> allFileList = fileListDao.findAllFileList(userId);
        //通讯录
        List<Director> directors = addressDao.selectDiscussnum();
        //邮件
        List<Mailreciver> noreadlist = mailService.findByReadAndDelAndReciverId(0, 0, userId);

        model.addAttribute("filenum", allFileList.size());
        model.addAttribute("directornum", directors.size());
        model.addAttribute("discussnum", noreadlist.size());

        List<AoaNoticeList> aoaNoticeLists = aoaNoticeListService.queryNoticeListById(userId, null, (1 - 1) * 3, 3);
        //公告
        model.addAttribute("list", aoaNoticeLists);
        //计划
        List<AoaPlanList> aoaPlanLists = aoaPlanList.selectAll(userId, null, 0, 3, null, null, null);

        model.addAttribute("planList", aoaPlanLists);
        //标签
        List<AoaNotepaper> notepaperlist = userPlaneServiceImp.queryNotepaper(userId, 0, 3);
        model.addAttribute("notepaperList", notepaperlist);
        model.addAttribute("user", aoaUser);
        //流程
        List<ProcessAll> all = auditService.findAll(userId, null, 0, 3);
        model.addAttribute("processlist", all);

        model.addAttribute("user", aoaUser);


        return "systemcontrol/control";
    }

}
